package com.D5Power.Editor.EditorWindow
{
	import com.D5Power.Objects.GameObject;
	import com.D5Power.utils.XYArray;
	
	import flash.events.Event;
	import flash.filesystem.File;

	
	import org.aswing.JButton;
	import org.aswing.JLabel;
	import org.aswing.JTextField;
	import org.aswing.ext.Form;
	
	public class MapAttr extends ObjectAttr
	{	
		protected var _tileX:JTextField;
		
		protected var _tileY:JTextField;
		
		protected var _mapSizeX:JTextField;
		
		protected var _mapSizeY:JTextField;
		
		protected var _work:uint = 1;
		
		protected var _mapid:JTextField;
		
		public static const NEWMAP:uint = 1;
		
		public static const EDITMAP:uint = 2;
		
		
		
		public function MapAttr(mod:uint,owner:D5RpgEditor, obj:GameObject=null)
		{
			_work = mod;
			mytitle = '地图属性';
			super(owner, obj);
		}
		
		override protected function init(mode:uint=0):void
		{
			
			_box = new Form();
			
			_mapid = new JTextField(_root.Shower==null ? '0' : _root.Shower.scene.Map.mapid.toString(),4);
			var lab3:JLabel = new JLabel('地图编号：');
			
			
			var lab0:JLabel = new JLabel('渲染尺寸：');
			
			
			_tileX = new JTextField(Global.TILE_SIZE.x.toString(),4);
			_tileY = new JTextField(Global.TILE_SIZE.y.toString(),4);
			
			if(_work==EDITMAP)
			{
				_tileX.setEditable(false);
				_tileY.setEditable(false);
			}
			
			var lab1:JLabel = new JLabel('区块规格：');
			
			_mapSizeX = new JTextField(Global.MAPSIZE.x.toString(),4);
			_mapSizeY = new JTextField(Global.MAPSIZE.y.toString(),4);
			if(_work==EDITMAP)
			{
				_mapSizeX.setEditable(false);
				_mapSizeY.setEditable(false);
			}
			var lab2:JLabel = new JLabel('地图尺寸：');
			
			_box.addRow(lab3,_mapid);
			_box.addRow(lab2,_mapSizeX,_mapSizeY);
			_box.addRow(lab1,_tileX,_tileY);
			
			_box.setSizeWH(200,160);
			getContentPane().append(_box);
			
			super.init();
		}
		
		override protected function onSubmit(e:Event):void
		{
			
			if(_work==EDITMAP)
			{
				super.onSubmit(e);
				return;
			}
			
			// 开始扫描目标文件夹
			var dirPath:String = 'asset/map/map'+int(_mapid.getText());
			var fp:File = File.applicationDirectory.resolvePath(dirPath);
			if(!fp.exists)
			{
				var file:File = new File(File.applicationDirectory.resolvePath(dirPath).nativePath);
				file.createDirectory();
				fp = File.applicationDirectory.resolvePath(dirPath);
				if(!fp.exists)
				{
					_root.msg('提示',"资源目录不存在！请检查！");
					return;
				}else{
					_root.msg('提示',"资源目录已自动创建！如果您需要向地图中新增\n相关建筑,NPC等，请把素材放置于"+dirPath);
				}
				
			}
			
			
			D5Power.mapid = int(_mapid.getText());
			
			
			Global.W = 750;
			Global.H = 500;
			
			
			var sx:uint = int(_mapSizeX.getText());
			var sy:uint = int(_mapSizeY.getText());
			if(sx*sy==0 || sx<Global.W || sy<Global.H)
			{
				_root.msg('错误','地图尺寸非法');
				return;
			}
			
			Global.MAPSIZE.x = sx;
			Global.MAPSIZE.y = sy;
			
			
			
			var tx:uint = int(_tileX.getText());
			var ty:uint = int(_tileY.getText());
			
			if(tx*ty==0 || tx>sx || ty>sy)
			{
				_root.msg('错误','区块尺寸非法');
				return;
			}
			
			Global.TILE_SIZE = new XYArray(tx,ty);
			
			_root.startMap();
			
			super.onSubmit(e);
		}
		
	}
}